#include "GTimeManager.h"


namespace GEngine
{
	// Description: Default constructor
	// Return:
	// Notes:
	// -----------------------------------------------------
	GTimeManager::GTimeManager()
	{
		UpdateTime = 0.0;
		FrameTime = 0.0;
	}

	// Description: Destructor
	// Return:
	// Notes:
	// -----------------------------------------------------
	GTimeManager::~GTimeManager()
	{
	}

	// Description: Stores the previous Core Update time and resets the timer
	// Return:
	// Notes: Stored Core time is used for updating current scene
	// -----------------------------------------------------
	void GTimeManager::AdvanceUpdateTime()
	{
		UpdateTime = UpdateTimer.GetElapsedSeconds();
		UpdateTimer.Reset();
	}

	// Description: Stores the previous Render time and resets the timer
	// Return:
	// Notes:
	// -----------------------------------------------------
	void GTimeManager::AdvanceFrameTime()
	{
		FrameTime = FrameTimer.GetElapsedSeconds();
		FrameTimer.Reset();
	}

	// Description: Returns the elapsed time since the engine is up
	// Return:
	// Notes:
	// -----------------------------------------------------
	GUInt64	GTimeManager::GetUpTime() const
	{
		return UpTimer.GetElapsedTime(GUtil::GTimeRatio::Second);
	}

	// Description: Returns the stored elapsed time of the last render operation
	// Return:
	// Notes:
	// -----------------------------------------------------
	double GTimeManager::GetFrameTime() const
	{
		return FrameTime;
	}

	// Description: Returns the stored elapsed time of the last core update operation
	// Return:
	// Notes:
	// -----------------------------------------------------
	double GTimeManager::GetUpdateTime() const
	{
		return UpdateTime;
	}

	// Description: Resets all timers that are registered
	// Return:
	// Notes:
	// -----------------------------------------------------
	void GTimeManager::ResetTimers()
	{
		TimerList::iterator End = Timers.end();
		TimerList::iterator Current = Timers.begin();

		while (Current != End)
		{
			(*Current)->Reset();

			Current++;
		}
	}

	// Description: removes all registered timers.
	// Return:
	// Notes:
	// -----------------------------------------------------
	void GTimeManager::ClearTimers()
	{
		Timers.clear();
	}

	// Description: Adds timer to the list so it wil lbe updated every frame
	// Return:
	// Notes:
	// -----------------------------------------------------
	void GTimeManager::AppendTimer(GUtil::GTimer* Timer)
	{
		Timers.push_back(Timer);
	}

	// Description: Removes timer from update list
	// Return:
	// Notes:
	// -----------------------------------------------------
	void GTimeManager::RemoveTimer(GUtil::GTimer* Timer)
	{
		Timers.remove(Timer);
	}

	// Description: updates the timers in the list
	// Return:
	// Notes:
	// -----------------------------------------------------
	void GTimeManager::Update()
	{
		TimerList::iterator End = Timers.end();
		TimerList::iterator Current = Timers.begin();

		while (Current != End)
		{
			(*Current)->Update();

			Current++;
		}
	}

	// Description: Initialization
	// Return:
	// Notes:
	// -----------------------------------------------------
	bool GTimeManager::Initialize() OVERRIDE
	{
		if (!GInitializable::Initialize())
		{
			return false;
		}

		UpdateTime = 0;
		UpdateTimer.Reset();
		
		FrameTime = 0;
		FrameTimer.Reset();

		UpTimer.Reset();

		return true;
	}

	// Description: Uninitializetion
	// Return:
	// Notes:
	// -----------------------------------------------------
	void GTimeManager::Uninitialize() OVERRIDE
	{
		ClearTimers();

		GInitializable::Uninitialize();
	}

} // namespace GCore